﻿(function () {
    var l = abp.localization.getResource('LanguageManagement');
    
    var _languageTextAppService = yunxSoft.abp.languageManagement.languageText;

    var _editModal = new abp.ModalManager(
        abp.appPath + 'LanguageManagement/LanguageTexts/EditModal'
    );

    var _dataTable = null;

    abp.ui.extensions.entityActions.get('languageManagement.languageTexts').addContributor(
        function(actionList) {
            return actionList.addManyTail(
                [
                    {
                        text: l('Edit'),
                        visible: abp.auth.isGranted(
                            'LanguageManagement.LanguageTexts.Update'
                        ),
                        action: function (data) {
                            _editModal.open({
                                id: data.record.id,
                            });
                        },
                    }
                ]
            );
        }
    );

    abp.ui.extensions.tableColumns.get('languageManagement.languageTexts').addContributor(
        function (columnList) {
            columnList.addManyTail(
                [
                    {
                        title: l("Actions"),
                        rowAction: {
                            items: abp.ui.extensions.entityActions.get('languageManagement.languageTexts').actions.toArray()
                        }
                    },
                    {
                        title: l("Key"),
                        data: 'name',
                    },
                    {
                        title: l("BaseValue"),
                        data: 'baseValue',
                    },
                    {
                        title: l("Value"),
                        data: 'value',
                    },
                    {
                        title: l("ResourceName"),
                        data: 'resourceName',
                    }
                ]
            );
        },
        0 //adds as the first contributor
    );

    $(function () {
        var _$wrapper = $('#LanguageTextsWrapper');
        var _$searchBtn = $('#search');
        
        _dataTable = _$wrapper.find('table').DataTable(
            abp.libs.datatables.normalizeConfiguration({
                order: [[1, 'asc']],
                processing: true,
                searching: false,
                paging: true,
                scrollX: true,
                serverSide: true,
                ajax: abp.libs.datatables.createAjax(_languageTextAppService.getList, function (requestData, dataTableSettings) {
                    return {
                        baseCultureName: $('#baseCultureNameInput').val(),
                        targetCultureName: $('#targetCultureNameInput').val(),
                        resourceName: $('#resourceNameInput').val(),
                        getOnlyEmptyValues: $('#getOnlyEmptyValuesInput').val(),
                        filter: $('#filterInput').val(),
                    };
                }),
                columnDefs: abp.ui.extensions.tableColumns.get('languageManagement.languageTexts').columns.toArray(),
            })
        );

        _editModal.onResult(function () {
            _dataTable.ajax.reloadEx();
        });

        _$searchBtn.click(() => {
            _dataTable.ajax.reloadEx();
        })
        
    });
})();